package com.app.controller 
{
	import org.component.item.ListItem;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.puremvc.as3.interfaces.INotification;

	import com.app.AppFacade;
	
	import com.app.view.AppMediator;
	import com.app.view.manager.LayerManagerMediator;
	import com.app.view.manager.SceneManagerMediator;
	import com.app.view.manager.DialogManagerMediator;
	import com.app.view.component.HallMediator;
	import com.app.view.component.RoomMediator;
	import com.app.view.component.scene.HallScene;
	
	import com.app.model.data.HallListItemData;
	import com.app.model.HallConnProxy;
	import com.app.model.RoomConnProxy;
	import com.app.model.GameConfigProxy;
	import com.app.model.RoomListProxy;
	import com.app.model.xml.AnalysisXMLProxy;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class HallCommand extends SimpleCommand implements ICommand
	{
		override public function execute(notification:INotification):void
		{
			var name:String = notification.getName();
			var params:* = notification.getBody();
			
			switch(name)
			{
				case AppFacade.INIT_HALL:
					initHall(params[0], params[1]);
					
					break;
				
				case AppFacade.LOGIN_HALL:
					loginHall();
					
					break;
					
				case AppFacade.ENTER_HALL:
					enterHall(params[0]);
					
					break;
					
				case AppFacade.ADD_NEW_ROOM:
					addNewRoom(params[0]);
					
					break;
					
				case AppFacade.UPDATE_ROOM:
					updateRoom(params[0]);
					
					break;
					
				case DialogManagerMediator.CREATEROOM_SEND_INFO:
					hallConnP.sendData("create_table", {name: params[0], min_pour: params[1], mid: gameConfigP.self.mid});
					
					break;
					
				case HallMediator.START_GAME_REQUEST:
					hallStartGameRequest();
					
					break;
					
				case AppFacade.HALL_START_GAME:
					joinRoom(params[0]);
					
					break;
				
				default:
				
					break;
			}
			
		}
		
		private function initHall(ip:String, port:Number):void
		{
			sendNotification(AppMediator.HALL_CONNECT_PROGRESS, [HallScene.NAME]);
			
			//TODO 连接大厅代码
			hallConnP.connect(ip, port);
		}
		
		private function loginHall():void
		{
			hallConnP.sendData('login', { mid: gameConfigP.self.mid } );
			
			roomConnP.close();
		}
		
		private function enterHall(data:Object):void
		{
			sendNotification(AppMediator.HALL_CONNECT_OK);
			
			parseRoomList(data);
			
			if (gameConfigP.serverid != 0 && gameConfigP.roomid != 0)
			{
				sendNotification(RoomMediator.INIT_ROOM_REQUEST, [gameConfigP.serverid, gameConfigP.roomid, false]);
				return;
			}
			
			sendNotification(SceneManagerMediator.JOIN_SCENE, ["HallScene", gameConfigP.getSwfUrl]);
			sendNotification(HallMediator.ADD_IN_LAYER, [gameConfigP.self, gameConfigP.getCurrentLevelExp(gameConfigP.self.exp, analysisXmlP.levels)]); //需要把下一个等级的经验值传过去
			sendNotification(HallMediator.SHOW_ROOM_LIST, [roomListP.roomList]);
		}
		
		private function parseRoomList(data:Object):void
		{
			var roomList:Array = [];
			
			var item:HallListItemData = null;
			for each(var d:Object in data)
			{
				item  = new HallListItemData();
				item.index = roomList.length;
				item.roomid = d.rid;
				item.name = d.name;
				item.minPour = d.min_pour;
				item.currMember = d.curr_member;
				item.odds = d.odds;
				item.sid = d.sid;
				roomList[item.index] = item;

				item = null;
			}	
			
			roomListP.roomList = roomList;
		}	
		
		private function addNewRoom(data:Object):void
		{
			var item:HallListItemData = new HallListItemData();
			item.index = data.rid;
			item.roomid = data.rid;
			item.minPour = data.min_pour;
			item.currMember = data.currMember;
			item.odds = data.odds;
			item.name = data.name;
			item.sid = data.sid;
			
			roomListP.addRoom(item);
			sendNotification(HallMediator.SHOW_ROOM_LIST, [roomListP.roomList]);
			
			if (data.mid == gameConfigP.self.mid)
			{
				sendNotification(HallMediator.HIDE_CRETAE_ROOM, [true, item.sid, item.roomid]);
			}
			
			item = null;
		}
		
		private function updateRoom(data:Object):void
		{
			var room:HallListItemData = roomListP.get(data.rid);
			if (room)
			{
				room.currMember = data.count;
				sendNotification(HallMediator.UPDATE_ROOM, [room]);
			}
		}
		
		private function hallStartGameRequest():void
		{
			hallConnP.sendData('quick_join_start_game', {mid: gameConfigP.self.mid});
		}
		
		private function joinRoom(data:Object):void
		{
			if (data.sid == 0 || data.rid == 0)
			{
				sendNotification(AppFacade.CLIENT_NOTICE, ["error_12"]);
				return;
			}
			
			sendNotification(RoomMediator.INIT_ROOM_REQUEST, [data.sid, data.rid, true]);
		}
		
		
		///////////////////////////////////////
		///// model 引用
		//////////////////////////////////////
		
		private function get hallConnP():HallConnProxy
		{
			return facade.retrieveProxy(HallConnProxy.NAME) as HallConnProxy;
		}
		
		private function get roomConnP():RoomConnProxy
		{
			return facade.retrieveProxy(RoomConnProxy.NAME) as RoomConnProxy;
		}
		
		private function get roomListP():RoomListProxy
		{
			return facade.retrieveProxy(RoomListProxy.NAME) as RoomListProxy;
		}
		
		private function get gameConfigP():GameConfigProxy
		{
			return facade.retrieveProxy(GameConfigProxy.NAME) as GameConfigProxy;
		}
		
		private function get analysisXmlP():AnalysisXMLProxy
		{
			return facade.retrieveProxy(AnalysisXMLProxy.NAME) as AnalysisXMLProxy;
		}
	}
}